<template>
  <div class="user-manage-container">
    <el-card class="header">
      <div>
        <el-button
          type="primary"
          @click="onImportExcelClick"
          v-permission="['importUser']"
        >
          {{ $t('msg.excel.importExcel') }}</el-button
        >
        <el-button @click="onToExcelClick" type="success">
          {{ $t('msg.excel.exportExcel') }}</el-button
        >
      </div>
    </el-card>
    <!-- 表格 -->
    <el-card>
      <el-table border :data="tableData" style="width: 100%">
        <!-- 序号 -->
        <el-table-column label="#" type="index"></el-table-column>
        <!-- 用户名 -->
        <el-table-column
          :label="$t('msg.excel.name')"
          prop="username"
        ></el-table-column>
        <!-- 手机号 -->
        <el-table-column
          :label="$t('msg.excel.mobile')"
          prop="mobile"
        ></el-table-column>
        <!-- 头像 -->
        <el-table-column :label="$t('msg.excel.avatar')" align="center">
          <template v-slot="{ row }">
            <el-image
              hide-on-click-modal
              preview-teleported
              class="avatar"
              :src="row.avatar"
              :preview-src-list="[row.avatar]"
            >
            </el-image>
          </template>
        </el-table-column>
        <!-- 角色 -->
        <el-table-column :label="$t('msg.excel.role')">
          <template #default="{ row }">
            <div v-if="row.role && row.role.length > 0">
              <el-tag v-for="item in row.role" :key="item.id" size="default">{{
                item.title
              }}</el-tag>
            </div>
            <div v-else>
              <el-tag size="default">{{ $t('msg.excel.default') }}</el-tag>
            </div>
          </template>
        </el-table-column>
        <!-- 时间 -->
        <el-table-column :label="$t('msg.excel.openTime')">
          <template #default="{ row }">
            {{ $filters.dateFilter(row.openTime) }}
          </template>
        </el-table-column>
        <!-- 操作 -->
        <el-table-column
          :label="$t('msg.excel.action')"
          width="260"
          fixed="right"
        >
          <template #default="{ row }">
            <el-button
              type="primary"
              size="default"
              @click="onShowClick(row._id)"
              >{{ $t('msg.excel.show') }}</el-button
            >
            <el-button
              @click="onShowRoleClick(row)"
              type="info"
              size="default"
              v-permission="['distributeRole']"
              >{{ $t('msg.excel.showRole') }}</el-button
            >
            <el-button
              type="danger"
              size="default"
              @click="onRemoveClick(row)"
              v-permission="['removeUser']"
              >{{ $t('msg.excel.remove') }}</el-button
            >
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        class="pagination"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page="page"
        :page-sizes="[2, 5, 10, 20]"
        :page-size="size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="total"
      >
      </el-pagination>
    </el-card>
    <ExportExcel v-model="exportToExcelVisiable" />
    <RolesDialog
      v-model="roleDialogVisible"
      :currentRoleTitle="currentRoleTitle"
      @updateRole="getListData"
    />
  </div>
</template>

<script setup>
import { onActivated, ref } from 'vue'
import { getUserManageList, deleteUser } from '@/api/user-manage'
import { watchSwitchLang } from '@/utils/i18n'
import { useRouter } from 'vue-router'
import { useI18n } from 'vue-i18n'
import { ElMessage, ElMessageBox } from 'element-plus'
import ExportExcel from './components/ExportExcel.vue'
import RolesDialog from './components/Roles.vue'

const tableData = ref([])
const page = ref(1)
const size = ref(2)
const total = ref(0)
const getListData = async () => {
  const res = await getUserManageList({
    page: page.value,
    size: size.value
  })
  tableData.value = res.list
  total.value = res.total
}
getListData()
watchSwitchLang(getListData)
onActivated(getListData)

const handleSizeChange = (currentSize) => {
  size.value = currentSize
  getListData()
}
const handleCurrentChange = (currentPage) => {
  page.value = currentPage
  getListData()
}

const router = useRouter()
const onImportExcelClick = () => {
  router.push('/user/import')
}

// 删除用户
const i18n = useI18n()
const onRemoveClick = (row) => {
  ElMessageBox.confirm(
    i18n.t('msg.excel.dialogTitle1') +
      row.username +
      i18n.t('msg.excel.dialogTitle2'),
    {
      type: 'warning'
    }
  )
    .then(async () => {
      await deleteUser(row._id)
      ElMessage.success(i18n.t('msg.excel.removeSuccess'))
      getListData()
    })
    .catch(() => {
      console.log('取消删除')
    })
}

const exportToExcelVisiable = ref(false)
const onToExcelClick = () => {
  exportToExcelVisiable.value = true
}

// 点击查看详情
const onShowClick = (id) => {
  router.push(`/user/info/${id}`)
}

// 绑定角色
const roleDialogVisible = ref(false)
const currentRoleTitle = ref([])
const onShowRoleClick = (row) => {
  roleDialogVisible.value = true
  currentRoleTitle.value = row.role
}
</script>

<style lang="scss" scoped>
.user-manage-container {
  .header {
    margin-bottom: 22px;
    text-align: right;
  }
  :deep .avatar {
    width: 60px;
    height: 60px;
    border-radius: 50%;
  }
  :deep .el-tag {
    margin-right: 6px;
  }
  .pagination {
    margin-top: 20px;
    text-align: center !important;
    justify-content: center;
  }
}
</style>
